package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.community.entity.OperationLog;
import com.wanmait.community.mapper.AdminMapper;
import com.wanmait.community.mapper.OperationLogMapper;
import com.wanmait.community.mapper.UserMapper;
import com.wanmait.community.service.OperationLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService{
    @Resource
    private OperationLogMapper operationLogMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AdminMapper adminMapper;
    @Override
    public List<OperationLog> findAll() {
        List<OperationLog> list = operationLogMapper.selectList(null);
        list.forEach(operationLog -> {
            operationLog.setAdmin(adminMapper.selectById(operationLog.getAdminId()));
            operationLog.setUser(userMapper.selectById(operationLog.getUserId()));
        });
        return list;
    }

    @Override
    public List<OperationLog> findByAdminId(Integer id) {
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<OperationLog>();
        queryWrapper.eq(id != null, OperationLog::getAdminId, id);
        List<OperationLog> list = operationLogMapper.selectList(queryWrapper);
        list.forEach(operationLog -> {
            operationLog.setAdmin(adminMapper.selectById(operationLog.getAdminId()));
            operationLog.setUser(userMapper.selectById(operationLog.getUserId()));
        });
        return list;
    }
}
